package cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail.vo.ReceiveOrderDetailPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail.vo.ReceiveOrderDetailRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.receiveorderdetail.vo.ReceiveOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.receiveorderdetail.ReceiveOrderDetailDO;
import cn.iocoder.yudao.module.hs.service.receiveorderdetail.ReceiveOrderDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收货单明细")
@RestController
@RequestMapping("/hs/receive-order-detail")
@Validated
public class ReceiveOrderDetailController {

    @Resource
    private ReceiveOrderDetailService receiveOrderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建收货单明细")
    @PreAuthorize("@ss.hasPermission('hs:receive-order-detail:create')")
    public CommonResult<Long> createReceiveOrderDetail(@Valid @RequestBody ReceiveOrderDetailSaveReqVO createReqVO) {
        return success(receiveOrderDetailService.createReceiveOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收货单明细")
    @PreAuthorize("@ss.hasPermission('hs:receive-order-detail:update')")
    public CommonResult<Boolean> updateReceiveOrderDetail(@Valid @RequestBody ReceiveOrderDetailSaveReqVO updateReqVO) {
        receiveOrderDetailService.updateReceiveOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收货单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:receive-order-detail:delete')")
    public CommonResult<Boolean> deleteReceiveOrderDetail(@RequestParam("id") Long id) {
        receiveOrderDetailService.deleteReceiveOrderDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收货单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:receive-order-detail:query')")
    public CommonResult<ReceiveOrderDetailRespVO> getReceiveOrderDetail(@RequestParam("id") Long id) {
        ReceiveOrderDetailDO receiveOrderDetail = receiveOrderDetailService.getReceiveOrderDetail(id);
        return success(BeanUtils.toBean(receiveOrderDetail, ReceiveOrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收货单明细分页")
    @PreAuthorize("@ss.hasPermission('hs:receive-order-detail:query')")
    public CommonResult<PageResult<ReceiveOrderDetailRespVO>> getReceiveOrderDetailPage(@Valid ReceiveOrderDetailPageReqVO pageReqVO) {
        return success(receiveOrderDetailService.getReceiveOrderDetailPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收货单明细 Excel")
    @PreAuthorize("@ss.hasPermission('hs:receive-order-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReceiveOrderDetailExcel(@Valid ReceiveOrderDetailPageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiveOrderDetailRespVO> list = receiveOrderDetailService.getReceiveOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收货单明细.xls", "数据", ReceiveOrderDetailRespVO.class, list);
    }

}